﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WastedMechanics
{
    class ObjectManager
    {
        public List<GameObject> Objects;
        public List<Character> CharactersList;
        Dictionary<string, Character> CharactersDict;
        
        public ObjectManager()
        {
            Objects = new List<GameObject>();
            CharactersList = new List<Character>();
            CharactersDict = new Dictionary<string, Character>();
        }

        public void Add(GameObject gameObject)
        {
            Objects.Add(gameObject);
        }

        public void Destroy(GameObject gameObject)
        {
            Objects.Remove(gameObject);
            gameObject.Destroy();
            if (gameObject is Character)
            {
                CharactersList.Remove(gameObject as Character);
                if (gameObject.Label != "" && CharactersDict.ContainsKey(gameObject.Label))
                    CharactersDict.Remove(gameObject.Label);
            }
        }

        public void Update()
        {
            for (int i = Objects.Count - 1; i >= 0; i--)             
                Objects[i].Update();
        }

        public Described NewDescribed(DescribedProfile profile)
        {
            Described newObject = new Described(profile);
            Add(newObject);
            return newObject;
        }

        public Described NewDescribed(Item item)
        {
            Described newObject = new Described(item);
            Add(newObject);
            return newObject;
        }

        public Character NewCharacter(CharacterProfile profile, string label="")
        {
            Character newObject = new Character(profile);
            newObject.Label = label;
            Add(newObject);
            CharactersList.Add(newObject);
            if (label != "") CharactersDict[label] = newObject;
            return newObject;
        }

        public Character GetCharacter(string label)
        {
            if (CharactersDict.ContainsKey(label))
                return CharactersDict[label];
            else
                return null;            
        }
    }
}
